package com.example.luckin.slice;

import com.example.luckin.ResourceTable;
import com.example.luckin.slice.fraction.*;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Text;

public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener  {
    private Text txtMain;
    private Text txtMe;
    private Text txtShoppingCart;
    private Text txtMenu;
    private Text txtOrder;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        // 初始化组件
        initBottomNavigation();
        // 显示切片
        ((FractionAbility)getAbility())
                .getFractionManager()
                .startFractionScheduler()
                .add(ResourceTable.Id_dl_frame,new MainFraction())
                .submit();
    }

    private void initBottomNavigation() {
        txtMain = (Text) findComponentById(ResourceTable.Id_txt_main);
        txtMenu = (Text) findComponentById(ResourceTable.Id_txt_menu);
        txtMe = (Text) findComponentById(ResourceTable.Id_txt_me);
        txtShoppingCart = (Text) findComponentById(ResourceTable.Id_txt_shoppingcart);
        txtOrder = (Text) findComponentById(ResourceTable.Id_txt_order);
        txtMain.setClickedListener(this);
        txtMenu.setClickedListener(this);
        txtMe.setClickedListener(this);
        txtShoppingCart.setClickedListener(this);
        txtOrder.setClickedListener(this);
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()){
            case ResourceTable.Id_txt_main:
                ((FractionAbility)getAbility())
                        .getFractionManager()
                        .startFractionScheduler()
                        .replace(ResourceTable.Id_dl_frame,new MainFraction())
                        .submit();
                break;
            case ResourceTable.Id_txt_menu:
                ((FractionAbility)getAbility())
                        .getFractionManager()
                        .startFractionScheduler()
                        .replace(ResourceTable.Id_dl_frame,new MenuFraction())
                        .submit();
                break;
            case ResourceTable.Id_txt_me:
                ((FractionAbility)getAbility())
                        .getFractionManager()
                        .startFractionScheduler()
                        .replace(ResourceTable.Id_dl_frame,new MeFraction())
                        .submit();
                break;
            case ResourceTable.Id_txt_shoppingcart:
                ((FractionAbility)getAbility())
                        .getFractionManager()
                        .startFractionScheduler()
                        .replace(ResourceTable.Id_dl_frame,new ShoppingCartFraction())
                        .submit();
                break;
            case ResourceTable.Id_txt_order:
                ((FractionAbility)getAbility())
                        .getFractionManager()
                        .startFractionScheduler()
                        .replace(ResourceTable.Id_dl_frame,new OrderFraction())
                        .submit();
                break;
        }
    }
}
